/**
 * Created by gongxun on 17/8/5.
 */

var HallBackgroupMove = function(){
    var that = {};

    that.root = null;
    that.mBgImage = null;
    that.mBgMovePoint = null;

    that.startMove = function(root, mBgImage){
        that.root = root;
        that.mBgImage = mBgImage;
        that.mBgMovePoint = cc.p(0, 0);

        var size = that.mBgImage.getContentSize();
        var minX = mo.curSize.x / 2 - (size.width / 2 - mo.curSize.x / 2);
        var maxX = mo.curSize.x / 2 + (size.width / 2 - mo.curSize.x / 2);
        var minY = mo.curSize.y / 2 - (size.height / 2 - mo.curSize.y / 2);
        var maxY = mo.curSize.y / 2 + (size.height / 2 - mo.curSize.y / 2);
        that.mBgImage.schedule(function (dt) {
            var speed = 20;
            var addPoint = cc.p(speed * that.mBgMovePoint.x, speed * that.mBgMovePoint.y);
            var p = that.mBgImage.getPosition();
            var toPoint = cc.pAdd(p, addPoint);
            toPoint.x = Math.max(toPoint.x, minX);
            toPoint.x = Math.min(toPoint.x, maxX);
            toPoint.y = Math.max(toPoint.y, minY);
            toPoint.y = Math.min(toPoint.y, maxY);
            that.mBgImage.setPosition(toPoint);
        });

        cc.inputManager.setAccelerometerEnabled(true);
        cc.eventManager.addListener({
            event: cc.EventListener.ACCELERATION,
            callback: function(acc, event){
                var speed = 1;

                // cc.log("acc.x:" + acc.x);
                // cc.log("acc.y:" + acc.y);
                // cc.log("acc.z:" + acc.z);
                var show = "acc.x:" + acc.x;
                show += "\nacc.y:" + acc.y;
                show += "\nacc.z:" + acc.z;

                //force x y
                that.mBgMovePoint = cc.p(0, 0);
                var xOffset = 0;
                if (acc.x < -0.1 || acc.x > 0.1){
                    xOffset = -acc.x
                }else{
                    xOffset = 0;
                }
                that.mBgMovePoint.x = speed * (xOffset);


                var yOffset = 0;
                if (acc.y < -0.72){
                    yOffset = (-acc.y - 0.72) * (1 / 0.28);
                }else if(acc.y > -0.6){
                    yOffset = (-acc.y - 0.72) * (1 / 1.72);
                }else {
                    yOffset = 0;
                }
                that.mBgMovePoint.y = speed * (yOffset);

                show += "\nxOffset:" + xOffset;
                show += "\nyOffset:" + yOffset;
            }
        }, that.root);
    };

    that.stopMove = function(){
        cc.eventManager.removeListeners(cc.EventListener.ACCELERATION);
    };

    return that;
};